package org.mclll.xwangplugin.datasource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.mclll.xwangplugin.log.GlobalLog;
import org.mclll.xwangplugin.log.LogUtil;
import org.mclll.xwangplugin.log.LoggerFactory;
import org.mclll.xwangplugin.town.database.mapper.manage.TownApplyManage;
import org.mclll.xwangplugin.town.database.mapper.manage.TownManage;
import org.mclll.xwangplugin.town.database.mapper.manage.TownPlayerManage;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

/**
 * @author: geduo
 * @date: 2023/9/22 11:49
 */
public class MybatisConfig {
    public static final String CONFIG_FILE_NAME = "mybatis-config.xml";

    public static LogUtil log;
    public static SqlSessionFactory SQL_SESSION_FACTORY;
    public static void init(File dataFolder) {
        log = LoggerFactory.get(GlobalLog.class);
        InputStream inputStream = null;
        try {
            File config = new File(dataFolder, CONFIG_FILE_NAME);
            inputStream = Files.newInputStream(config.toPath());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        SQL_SESSION_FACTORY = new SqlSessionFactoryBuilder().build(inputStream);

        try {
            TownManage.selectList(null);
        } catch (Exception e) {
            TownManage.createTable();
        }
        try {
            TownPlayerManage.selectList(null);
        } catch (Exception e) {
            TownPlayerManage.createTable();
        }
        try {
            TownApplyManage.selectList(null);
        } catch (Exception e) {
            TownApplyManage.createTable();
        }

    }

}
